import { Fields, TableAction } from "@/types/public";
import { Button, Popconfirm, Space } from "antd";
import React from "react";
import { EditOutlined, DeleteOutlined } from "@ant-design/icons";

// 设备分组数据结构定义
export interface registerGroupField {
  /** 分组唯一标识符，自增主键 */
  id: number;

  /** 分组名称（中文） */
  groupName: string;

  /** 分组名称（英文） */
  groupNameEn: string;

  /** 设备注册ID */
  deviceRegisterId: number;

  /** 设备分组ID */
  registerGroupFieldsId: number;

  /** 父分组ID */
  parentId: number;

  /** 分组状态：0-禁用，1-启用 */
  status: number;

  /** 记录创建时间 */
  createAt: string | Date;

  /** 记录最后更新时间 */
  updatedAt: string | Date;

  /** 备注信息 */
  remarks: string;

  children?: registerGroupField[];
}

export const createregisterGroupField = async (
  tableAction: TableAction<registerGroupField> = {},
  existingGroups: registerGroupField[] = [], // 已有分组数据用于父分组选择
  extendProps?: any
): Promise<Fields[]> => {
  return [
    {
      valueType: "number",
      inputType: "input-number",
      key: "id",
      title: "ID",
      dataIndex: "id",
      placeholder: "分组唯一标识",
      disabled: true,
      width: 120,
      rules: [{ type: "number", min: 1, message: "ID必须大于0" }],
    },
    {
      valueType: "string",
      inputType: "input",
      title: "分组名称（中文）",
      dataIndex: "groupName",
      key: "groupName",
      placeholder: "请输入中文分组名称",
      rules: [
        { required: true, message: "中文分组名称不能为空" },
        { max: 100, message: "分组名称长度不能超过100字符" },
      ],
    },
    {
      valueType: "string",
      inputType: "input",
      title: "分组名称（英文）",
      dataIndex: "groupNameEn",
      key: "groupNameEn",
      placeholder: "请输入英文分组名称",
      rules: [
        { required: false },
        { max: 100, message: "英文分组名称长度不能超过100字符" },
      ],
    },
    {
      valueType: "number",
      inputType: "input-number",
      title: "父分组ID",
      dataIndex: "parentId",
      key: "parentId",
      placeholder: "0或NULL表示根级分组",
      rules: [{ required: false }],
    },
    {
      valueType: "number",
      inputType: "select",
      title: "分组状态",
      dataIndex: "status",
      key: "status",
      placeholder: "请选择分组状态",
      options: [
        { value: 0, label: "禁用" },
        { value: 1, label: "启用" },
      ],
    },
    // {
    //   valueType: "string",
    //   inputType: "date-picker",
    //   title: "创建时间",
    //   dataIndex: "createAt",
    //   key: "createAt",
    //   disabled: true,
    //   placeholder: "记录创建时间",
    //   rules: [{ required: false }],
    // },
    // {
    //   valueType: "string",
    //   inputType: "date-picker",
    //   title: "更新时间",
    //   dataIndex: "updatedAt",
    //   key: "updatedAt",
    //   disabled: true,
    //   placeholder: "记录最后更新时间",
    //   rules: [{ required: false }],
    // },
    {
      valueType: "string",
      inputType: "textarea",
      title: "备注信息",
      dataIndex: "remarks",
      key: "remarks",
      placeholder: "请输入备注信息",
      autoSize: { minRows: 3, maxRows: 6 },
      rules: [{ required: false }],
    },
    {
      title: "操作",
      key: "action",
      fixed: "right",
      width: 150,
      render: (_: any, record: registerGroupField) => (
        <Space size="middle">
          <Button
            type="link"
            size="small"
            icon={<EditOutlined />}
            onClick={() => tableAction.edit?.(record)}
          >
            编辑
          </Button>
          <Popconfirm
            title="确定删除此设备分组？"
            description="删除分组将影响相关设备"
            onConfirm={() => tableAction.delete?.([record.id])}
          >
            <Button type="link" danger icon={<DeleteOutlined />}>
              删除
            </Button>
          </Popconfirm>
        </Space>
      ),
    },
  ];
};

// 获取分组状态选项的辅助函数
export const getStatusOptions = () => [
  { value: 0, label: "禁用" },
  { value: 1, label: "启用" },
];

// 获取根分组选项的辅助函数（用于父分组选择）
export const getParentGroupOptions = (groups: registerGroupField[]) => {
  return groups
    .filter((group) => group.status === 1) // 只显示启用的分组
    .map((group) => ({
      value: group.id,
      label: `${group.groupName} (${group.groupNameEn || "无英文名"})`,
    }));
};
